import win32serviceutil
import win32service
import win32event
import winerror
import servicemanager
import time
import sys
import os

class IOVServiceWSGIWindows(win32serviceutil.ServiceFramework):



    """
    #1.安装服务
    python IOVServiceWSGIWindows.py install

    #2.让服务自动启动
    python IOVServiceWSGIWindows.py --startup auto install

    #3.启动服务
    python IOVServiceWSGIWindows.py start

    #4.重启服务
    python IOVServiceWSGIWindows.py restart

    #5.停止服务
    python IOVServiceWSGIWindows.py stop

    #6.删除/卸载服务
    python IOVServiceWSGIWindows.py remove
    """

    _svc_name_ = "IOVServiceWSGIWindows"  # 服务名
    _svc_display_name_ = "IOV Data RealTime"  # 服务在windows系统中显示的名称
    _svc_description_ = "车联网实时数据服务"  # 服务的描述


    def __init__(self, args):
        win32serviceutil.ServiceFramework.__init__(self, args)
        self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)
        self.isAlive = True
        self._poll_intvl = 2

    def SvcDoRun(self):
        while self.isAlive:
            print ('IOVservice SvcDoRun')
            os.system('python D:\\PycharmProjects\\untitled\\IOVServiceWSGI.py')
            time.sleep(self._poll_intvl)

    def SvcStop(self):
        self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
        win32event.SetEvent(self.hWaitStop)
        self.isAlive = False

if __name__ == "__main__":
    if len(sys.argv) == 1:
        try:
            evtsrc_dll = os.path.abspath(servicemanager.__file__)
            servicemanager.PrepareToHostSingle(IOVServiceWSGIWindows)
            servicemanager.Initialize('IOVServiceWSGIWindows', evtsrc_dll)
            servicemanager.StartServiceCtrlDispatcher()
        except win32service.error:
            print("IOVservice Main except")
    else:
        win32serviceutil.HandleCommandLine(IOVServiceWSGIWindows)  # 括号里参数可以改成其他名字，但是必须与class类名一致；
